const WXAPI = require('../../wxapi/domain')
const CONFIG = require('../../config.js')
const app = getApp()

Page({
  data: {
    swiperCurrent: 0, //当前banner所在位置
    bannerList: [],
    actionList: [],
    pageNum:1,
    pageSize:4,
    orderBy: "action_date desc",
    refresh:false,
    openId: ''
  },
  onShow(){
    const _this = this
    WXAPI.banners().then(function (res) {
      if (res.code === 0) {
        _this.setData({
          bannerList: res.data
        })
      }
    }),
      WXAPI.queryActionList({
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize,
      orderBy: this.data.orderBy,
      }).then(function (res) {
        wx.hideLoading()
        const openid = wx.getStorageSync('openid');
        if (res.code === 0) {
          let actionList = []
          for (var i = 0; i < res.data.length; i++) {
            actionList.push(res.data[i]);
          }
          _this.setData({
            loadingMoreHidden: true,
            actionList: actionList,
            openId: openid
          });
        }else{

        }
      })
  },
  swiperchange: function(e) { // banner滚动事件
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  tapBanner: function(e) {
    if (e.currentTarget.dataset.id != 0) {
      wx.navigateTo({
        url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
      })
    }
  },
  bindTypeTap: function(e) {
    this.setData({
      selectCurrent: e.index
    })
  },
  onLoad: function(e) {
  },
  onPageScroll(e) {
    let scrollTop = this.data.scrollTop
    this.setData({
      scrollTop: e.scrollTop
    })
  },
  onShareAppMessage: function() {
    return {
      title: wx.getStorageSync('mallName'),
      path: '/pages/index/index?inviter_id=' + wx.getStorageSync('uid'),
      success: function(res) {
        // 转发成功
      },
      fail: function(res) {
        // 转发失败
      }
    }
  },
  getNotice: function() {
    var that = this;
    WXAPI.noticeList({pageSize: 5}).then(function (res) {
      if (res.code == 0) {
        that.setData({
          noticeList: res.data
        });
      }
    })
  },
  toSearch: function() {
    this.setData({
      curPage: 1
    });
    this.getGoodsList(this.data.activeCategoryId);
  },
  // 以下为搜索框事件
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false
    });
  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  },
  goMap(e){ // 打开地图
    const id = e.currentTarget.dataset.id
    const item = this.data.actionList.find(ele => {
      return ele.id == id
    })
    wx.openLocation({
      latitude: item.latitude,
      longitude: item.longitude,
      name: item.name,
      address: item.address
    })
  },
  callPhone(e){
    const tel = e.currentTarget.dataset.tel
    wx.makePhoneCall({
      phoneNumber: tel
    })
  },
  onPullDownRefresh: function () {
    
    var that = this;
    console.log(that.data.refresh)
    if (!that.data.refresh) {
      
      that.data.refresh = true;
      // 显示刷新动画
      wx.showToast({
        title: '刷新数据中',
        icon: 'loading'
      })
      WXAPI.queryActionList({
        pageNum: that.data.pageNum,
        pageSize: that.data.pageSize,
        orderBy: that.data.orderBy,
      }).then(function (res) {
        wx.hideLoading()
        that.data.refresh = false;
        if (res.code === 0) {
          let actionList = []
          for (var i = 0; i < res.data.length; i++) {
            actionList.push(res.data[i]);
          }
          that.setData({
            loadingMoreHidden: true,
            actionList: actionList,
            pageNum: 1,
          });
        } else {
          wx.showToast({
            title: '连接服务器失败',
            icon: 'loading'
          })
        }
      })

    } else {
      console.log("正在刷新...");
    }
  },
  onReachBottom: function (e) {
    var that = this;
    // 加载更多 loading
    wx.showToast({
      title: '数据获取中',
      icon: 'loading'
    })
    var pageNum = that.data.pageNum + 1
    var pageSize = that.data.pageSize
    var orderBy = that.data.orderBy
    WXAPI.queryActionList({
      pageNum: pageNum,
      pageSize: pageSize,
      orderBy: orderBy
    }).then(function (res) {
      wx.hideLoading()
      if (res.code === 0) {
        let actionList = that.data.actionList
        if (res.data.length<=0){
          wx.showToast({
            title: '已经没有数据了',
            icon: 'none'
          })
          return
        }
        for (var i = 0; i < res.data.length; i++) {
          actionList.push(res.data[i]);
        }
        that.setData({
          loadingMoreHidden: true,
          actionList: actionList,
          pageNum: pageNum
        });
      } else {
        wx.showToast({
          title: '连接服务器失败',
          icon: 'loading'
        })
      }
    })
    
    
  }
})